package com.sgcc.ln.pmos.common.utils;

import android.content.Context;
import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.sgcc.ln.pmos.common.bean.UpdateInfoBean;
import com.sgcc.ln.pmos.common.bean.UpdateRootBean;
import com.sgcc.ln.pmos.common.dialog.UpdateAppDialog;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvcNetCallback;

import java.util.HashMap;
import java.util.Map;

public class UpdateAppUtil {


    public static void update(Context mContext) {
//        SPUtils spUtils = SPUtils.getInstance();
//        spUtils.put("versionHeader", 1);
//        spUtils.put("versionBody", 0.9f);

        Map<String, Object> params = new HashMap<>();
        params.put("type", "2");//1 ios  2 android
        params.put("versionName", AppUtils.getAppVersionName());
//        params.put("versionName", "1.3.0");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.UPDATE_APP,
                new MvcNetCallback<UpdateInfoBean>() {
                    @Override
                    public void onResponse(UpdateInfoBean response, int id) {
                        super.onResponse(response, id);
                        if (response != null) {
                            if (response.getCode() == 200) {
                                if (null == response.getData())
                                    return;

                                if (TextUtils.equals(response.getData().getResult(), "强制更新")) {
                                    new XPopup.Builder(mContext)
                                            .isDestroyOnDismiss(true)
                                            .dismissOnBackPressed(false)
                                            .dismissOnTouchOutside(false)
                                            .asCustom(new UpdateAppDialog(mContext, response, true))
                                            .show();
                                } else if (TextUtils.equals(response.getData().getResult(), "更新")) {
                                    new XPopup.Builder(mContext)
                                            .isDestroyOnDismiss(true)
                                            .dismissOnBackPressed(false)
                                            .dismissOnTouchOutside(true)
                                            .asCustom(new UpdateAppDialog(mContext, response, false))
                                            .show();
                                } else if (TextUtils.equals(response.getData().getResult(), "不更新")) {

                                } else {
                                    ToastUtils.showShort(response.getMsg());
                                }
                            } else {
                                ToastUtils.showShort(response.getMsg());
                            }
                        } else {
                            ToastUtils.showShort("获取更新信息失败");
                        }


//                        if (null != response && response.getData() != null && response.getData().size() > 0) {
//                            UpdateRootBean.DataDTO updateAppBean = response.getData().get(0);
//                            boolean isForce = false;//是否强制更新
//                            boolean isNeed = false;//是否需要更新
//                            int versionHeader = spUtils.getInt("versionHeader");
//                            float versionBody = spUtils.getFloat("versionBody");
//                            if (updateAppBean.getVersionHeader() > versionHeader) {
//                                isForce = true;
//                                isNeed = true;
//                            } else {
//                                if (updateAppBean.getVersionBody() > versionBody) {
//                                    isForce = false;
//                                    isNeed = true;
//                                }
//                            }
//                            if (isNeed) {
//                                new XPopup.Builder(mContext)
//                                        .isDestroyOnDismiss(true)
//                                        .dismissOnBackPressed(!isForce)
//                                        .dismissOnTouchOutside(!isForce)
//                                        .asCustom(new UpdateAppDialog(mContext, updateAppBean, isForce))
//                                        .show();
//                            }
//                        }

                    }
                });


    }

}